//
//  Extensions.swift
//  QualityControlSystem
//
//  Created by Daniel Hu on 12/26/16.
//  Copyright © 2016 com.mask. All rights reserved.
//

import UIKit
import Photos
import SVProgressHUD

extension Notification.Name {
    static let userDidLogedin = Notification.Name("userDidLogedin")
    static let tableViewDidSelect = Notification.Name("tableViewDidSelect")
}

extension UIColor {
    static func colorFrom(r: UInt, g: UInt, b: UInt, a: CGFloat) -> UIColor {
        return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: a)
    }
}

extension UITextField {
    static func textFieldWith(placeholder: String, leftViewImageName: String) -> UITextField {
        let tf: UITextField = UITextField()
        tf.autocorrectionType = UITextAutocorrectionType.no
        tf.autocapitalizationType = UITextAutocapitalizationType.none
        
        // add a bottom line
        let bottomLine = UIView()
        bottomLine.backgroundColor = UIColor.white
        tf.addSubview(bottomLine)
        bottomLine.snp.makeConstraints { (make) in
            make.width.bottom.left.equalTo(tf);
            make.height.equalTo(0.5);
        }
        
        // configure textfield properties
        tf.textAlignment = NSTextAlignment.left
        tf.clearButtonMode = UITextFieldViewMode.whileEditing
        tf.textColor = UIColor.white
        tf.font = UIFont.systemFont(ofSize: 15)
        let attributes: [String: Any] = [NSForegroundColorAttributeName: UIColor.white, NSFontAttributeName: UIFont.systemFont(ofSize: 15)]
        tf.attributedPlaceholder = NSAttributedString.init(string: placeholder, attributes: attributes)
        
        // configure left view
        let leftView = UIImageView(frame: CGRect.init(x: 0, y: 0, width: 50, height: 50))
        leftView.image = UIImage.init(named: leftViewImageName)
        tf.leftView = leftView
        tf.leftViewMode = UITextFieldViewMode.always
        
        return tf
    }
}

extension UIButton {
    static func loginButton() -> UIButton {
        let loginBtn: UIButton = UIButton.init(type: .custom)
        loginBtn.setTitle("登 录", for: UIControlState.normal)
        loginBtn.setTitleColor(UIColor.white, for: UIControlState.normal)
        loginBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        loginBtn.layer.cornerRadius = 6 * UIScreen.main.bounds.size.height / 667.0
        loginBtn.backgroundColor = Color.bg
        loginBtn.layer.borderWidth = 1
        loginBtn.layer.borderColor = UIColor.white.cgColor
        return loginBtn
    }
}

extension Array {
    
    func needsSendNetWorkRequest() -> Bool {
        for foo in self {
            let process = foo as! SubProjectProcess
            if let _ = process.asset {
                return true
            }
        }
        return false
    }
    
    func uploadAllPicturesWith(projectID: Int, subProjectID: Int, completion: @escaping([String: Any]) -> Void) {
        SVProgressHUD.show(withStatus: "上传图片...")
        var pictures = [Data]()
        let imageOptions = PHImageRequestOptions()
        imageOptions.isSynchronous = true
        for item in self {
            let process = item as! SubProjectProcess
            PHImageManager.default().requestImageData(for: process.asset!, options: imageOptions, resultHandler: { ( data, string, orientation, info ) in
                pictures.append(data!)
            })
        }
        HTTPRequest.subProjectUploadAllPicture(pictures: pictures, projectId: projectID, subProjectId: subProjectID, completion: { response in
            
            SVProgressHUD.dismiss()
        })
    }
    
    func uploadNextImage() {
        let imageOptions = PHImageRequestOptions()
        imageOptions.isSynchronous = true
        var image: UIImage? = nil
        PHImageManager.default().requestImage(for: nextImageAssetToUpload(), targetSize: CGSize(width: Screen.width, height: Screen.height), contentMode: .aspectFill, options: imageOptions, resultHandler: {result, info in
            image = result
        })
        var imageData = UIImageJPEGRepresentation(image!, 0.2)
        if imageData == nil {
            imageData = UIImagePNGRepresentation(image!)
        }
        if imageData == nil {
            SVProgressHUD.showError(withStatus: "图片格式错误")
        }
//        HTTPRequest.subProjectUploadAllPicture(pictures: <#T##[String]#>, id: <#T##Int#>, subProjectId: <#T##Int#>, projectId: <#T##Int#>, completion: <#T##(Dictionary<String, Any>?) -> Void#>)
    }
    
    func nextImageAssetToUpload() -> PHAsset{
        let process = self[1] as! SubProjectProcess
        return process.asset!
    }
}
